import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import 'dart:convert' as convert;

Widget buildView(
    AccountInfoViewState state, Dispatch dispatch, ViewService viewService) {
  Widget _userInfo() {
    List info = [
      {'name': '姓名', 'value': state.model.name},
      {'name': '户号', 'value': state.model.accountNo},
      {'name': '联系方式', 'value': state.model.phone},
      {'name': '身份证号', 'value': state.model.idNo},
      {'name': '地址', 'value': state.model.address},
    ];
    List<Widget> widgets = [];
    for (int i = 0; i < info.length; ++i) {
      var item = info[i];
      Widget row = Container(
        padding: EdgeInsets.only(bottom: 12, top: 12),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              item['name'],
              style: TextStyle(
                color: Color(0xFF999999),
                fontSize: 14,
                fontWeight: FontWeight.w500,
                height: 1,
              )
            ),
            Expanded(
              child: Container(),
            ),
            Container(
              width: 180,
              child: Text(
                item['value'],
                style: TextStyle(
                  color: Color(0xFF222222),
                  fontSize: 13,
                  fontWeight: FontWeight.w500,
                  height: 1
                ),
                softWrap: true,
                maxLines: 3,
                textAlign: TextAlign.right,
              ),
            ),
            i == 1
                ? InkWell(
                    child: Container(
                      margin: EdgeInsets.only(left: 10),
                      color: Colors.white,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(top: 1),
                            child: Container(
                              width: 1,
                              height: 10,
                              color: Color(0xff999999),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 10, right: 2),
                            child: Text(
                              '复制',
                              style: TextStyle(
                                  color: Color(0xFF4D81EF),
                                  fontSize: 13,
                                  fontWeight: FontWeight.w500,
                                  height: 1
                              ),

                            ),
                          ),
                        ],
                      ),
                    ),
                    onTap: () =>
                        dispatch(AccountInfoViewActionCreator.onCopyNo()),
                  )
                : Container()
          ],
        ),
      );
      widgets.add(row);
    }

    widgets.insert(
      0,
      Padding(
        padding: EdgeInsets.only(bottom: 4),
        child: Text(
          '用户信息',
          style: TextThemeConfig.Font16BoldWithColor22,
        ),
      ),
    );

    return Container(
      margin: EdgeInsets.only(left: 15, top: 10, right: 15),
      padding: EdgeInsets.only(left: 15, top: 20, right: 17, bottom: 4),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(6)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    );
  }

  String gasPrice(String price) {
    var info = convert.jsonDecode(price);
    if (info is Map) {
      Map first = info['First'];
      Map two = info['Two'];
      Map three = info['Three'];
      return '阶梯价(元/m³)：一阶:${first['GasPrice']} / ${first['GasCubic']} \n\r二阶:${two['GasPrice']} / ${two['GasCubic']} \n\r 三阶:${three['GasPrice']} / ${three['GasCubic']}';
    }
    return '正常价(元/m³)：$price';
  }

  Widget _gasInfo() {
    List info = [
      {'name': '通气时间', 'value': state.model.gasOpenTime},
      {'name': '燃气表号', 'value': state.model.gasNo},
      {'name': '燃气定价', 'value': gasPrice(state.model.gasPrice)},
      {'name': '表具厂家', 'value': state.model.gasFactory},
      {'name': '表具类型', 'value': state.model.gasType},
    ];
    List<Widget> widgets = [];
    for (int i = 0; i < info.length; ++i) {
      var item = info[i];
      Widget row = Container(
        padding: EdgeInsets.only(bottom: 12, top: 12),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              item['name'],
              style: TextThemeConfig.Font14WithColor99,
            ),
            Expanded(
              child: Container(),
            ),
            Container(
              width: 220,
              child: Text(
                item['value'],
                style: TextThemeConfig.Font13WithColor22,
                softWrap: true,
                maxLines: 5,
                textAlign: TextAlign.right,
              ),
            ),
          ],
        ),
      );
      widgets.add(row);
    }

    widgets.insert(
      0,
      Padding(
        padding: EdgeInsets.only(bottom: 4),
        child: Text(
          '燃气表具信息',
          style: TextThemeConfig.Font16BoldWithColor22,
        ),
      ),
    );

    return Container(
      margin: EdgeInsets.only(left: 15, top: 10, right: 15),
      padding: EdgeInsets.only(left: 15, top: 20, right: 17, bottom: 4),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(6)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    );
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: '用户信息', actions: [
      GestureDetector(
        behavior: HitTestBehavior.translucent,
        child: Container(
          padding: EdgeInsets.only(right: 12),
          height: 40,
          child: Center(
            child: Text(
              state.isDel ? '解绑' : '绑定',
              style: TextThemeConfig.ActionBarTitle,
            ),
          ),
        ),
        onTap: () => dispatch(AccountInfoViewActionCreator.onSubmit()),
      ),
    ]),
    backgroundColor: ThemeConfig.separeColor,
    body: state.model == null
        ? MyLoadingWidget()
        : SingleChildScrollView(
            child: Column(
              children: [
                _userInfo(),
                _gasInfo(),
              ],
            ),
          ),
  );
}
